const http = require('http');
const fs = require('fs');
const mime = require('mime');
http.createServer((req, res) => {
    const urlString = req.url;
    if (urlString == '/favicon.ico') {
        res.end()
        return
    };

    // urlString.split('.')[1] 获取后缀
    const type = mime.getType(urlString.split('.')[1]); // 获取到对应文件后缀后，再获取content-type类型
    console.log(type);
    res.writeHead(200, {
        'content-type': type // 设置 content-type
    });

    const file = fs.readFileSync(`.${urlString}`); // 读取文件

    res.end(file);
}).listen(8080);

